package com.learn.composite.safe;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>项目名称: design-pattern </p>
 *
 * <p>文件名称: SLeaf </p>
 *
 *
 */
@Slf4j
public class SComposite extends SComponent {

    public SComposite(String name) {
        super(name);
    }

    private List<SComponent> children = new ArrayList<>();

    public void add(SComponent component) {
        children.add(component);
    }

    public void remove(SComponent component) {
        children.remove(component);
    }

    @Override
    public void display(int depth) {
        //输出树形结构
        for(int i=0; i<depth; i++) {
            System.out.print("- ");
         }
        System.out.println(name);

        //下级遍历
        for (SComponent component : children) {
             component.display(depth + 1);
         }
    }
}
